package cn.iocoder.yudao.module.famous.doctor.controller.admin.forum;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.forum.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.forum.FamForumDO;
import cn.iocoder.yudao.module.famous.doctor.convert.forum.FamForumConvert;
import cn.iocoder.yudao.module.famous.doctor.service.forum.FamForumService;

@Tag(name = "管理后台 - 论坛表")
@RestController
@RequestMapping("/famous.doctor/fam-forum")
@Validated
public class FamForumController {

    @Resource
    private FamForumService famForumService;

    @PostMapping("/create")
    @Operation(summary = "创建论坛表")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:create')")
    public CommonResult<Long> createFamForum(@Valid @RequestBody FamForumCreateReqVO createReqVO) {
        return success(famForumService.createFamForum(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新论坛表")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:update')")
    public CommonResult<Boolean> updateFamForum(@Valid @RequestBody FamForumUpdateReqVO updateReqVO) {
        famForumService.updateFamForum(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除论坛表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:delete')")
    public CommonResult<Boolean> deleteFamForum(@RequestParam("id") Long id) {
        famForumService.deleteFamForum(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得论坛表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:query')")
    public CommonResult<FamForumRespVO> getFamForum(@RequestParam("id") Long id) {
        FamForumDO famForum = famForumService.getFamForum(id);
        return success(FamForumConvert.INSTANCE.convert(famForum));
    }

    @GetMapping("/list")
    @Operation(summary = "获得论坛表列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:query')")
    public CommonResult<List<FamForumRespVO>> getFamForumList(@RequestParam("ids") Collection<Long> ids) {
        List<FamForumDO> list = famForumService.getFamForumList(ids);
        return success(FamForumConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得论坛表分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:query')")
    public CommonResult<PageResult<FamForumRespVO>> getFamForumPage(@Valid FamForumPageReqVO pageVO) {
        PageResult<FamForumDO> pageResult = famForumService.getFamForumPage(pageVO);
        return success(FamForumConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出论坛表 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:fam-forum:export')")
    @OperateLog(type = EXPORT)
    public void exportFamForumExcel(@Valid FamForumExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FamForumDO> list = famForumService.getFamForumList(exportReqVO);
        // 导出 Excel
        List<FamForumExcelVO> datas = FamForumConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "论坛表.xls", "数据", FamForumExcelVO.class, datas);
    }

}
